package com.webserver.controller;


import com.webserver.http.HttpRequest;
import com.webserver.http.HttpResponse;
import com.webserver.vo.Article;
import com.webserver.vo.User;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.URI;

public class ArticleController {
    private static String userDirName="./article";
    static {
        File userFile = new File(userDirName);
        if (!userFile.exists()) {
            userFile.mkdirs();
        }
    }


    public void writeArticle(HttpRequest request, HttpResponse response){
        String title=request.getParameter("title");
        String content=request.getParameter("content");

        if (title==null||content==null){
            response.setEntity(new File("./webapps/myweb/article_fail.html"));
            return;
        }

        File userFile = new File(userDirName+title+".obj");
        if(userFile.exists()){
            response.setEntity(new File("./webapps/myweb/article.html"));
            return;
        }
        Article article = new Article(title,content);
        try(
                ObjectOutputStream oos = new ObjectOutputStream(
                        new FileOutputStream(userDirName+title+".obj")
                );
        ){
            oos.writeObject(article);
            response.setEntity(new File("./webapps/myweb/article_success.html"));

        }catch(IOException e){
            e.printStackTrace();
        }




        System.out.println("用户注册处理完毕!!!!");
    }
}
